<template>
  <div>
    <el-card class="login-form-layout">
      <el-form autoComplete="on"
               :model="loginForm"
               :rules="loginRules"
               ref="loginForm"
               label-position="left">
        <div style="text-align: center">
          <svg-icon icon-class="ums-admin" style="width: 56px;height: 56px;color: #409EFF"></svg-icon>
        </div>
        <h2 class="login-title color-main">测试AICode项目</h2>
        <div>
          <el-form-item prop="username">
            <el-input name="username"
                      type="text"
                      v-model="loginForm.username"
                      placeholder="请输入用户名">
          <span slot="prefix">
            <svg-icon icon-class="user" class="color-main"></svg-icon>
          </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input name="password"
                      :type="pwdType"
                      @keyup.enter.native="handleLogin"
                      v-model="loginForm.password"
                      autoComplete="on"
                      placeholder="请输入密码">
          <span slot="prefix">
            <svg-icon icon-class="password" class="color-main"></svg-icon>
          </span>
              <span slot="suffix" @click="showPwd">
            <svg-icon icon-class="eye" class="color-main"></svg-icon>
          </span>
            </el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-input name="captcha"
                      type="text"
                      v-model="loginForm.captcha"
                      maxlength="4"
                      placeholder="请输入验证码" style="width: 48%">
               <span slot="prefix">
            <svg-icon icon-class="ums" class="color-main"></svg-icon>
          </span>
            </el-input>
            <div style="width: 50%;display: inline;float: right;text-align: right">
              <img :src="imgSrc"
                   ref="code" @click="changeCode" style="height: 40px;width: 100%"/>
            </div>
          </el-form-item>
          <el-form-item style="margin-bottom: 10px;text-align: center">
            <el-button style="width: 80%" type="primary" :loading="loading" @click.native.prevent="handleLogin">
              登录
            </el-button>
          </el-form-item>
          <el-form-item style="margin-bottom: 10px;text-align: center">
            <el-button style="width: 80%" type="success" :loading="loading"
                       @click.native.prevent="dialogFormVisible = true">
              注册
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
    <img :src="login_center_bg" class="login-center-layout">

    <!--    注册弹框-->
    <el-dialog title="注册" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input size="mini" v-model="registerForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input size="mini" v-model="registerForm.nickName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="mini" v-model="registerForm.password" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input size="mini" v-model="registerForm.confirmPassword" autocomplete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input size="mini" v-model="registerForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input size="mini" v-model="registerForm.email" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleRegister">确 定</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import {isValidateCaptcha, isvalidPass, isvalidUsername} from '@/utils/validate'
import login_center_bg from '@/assets/images/login_center_bg.png'
import {getBaseUri} from '@/utils/auth'
import {userPostRequire} from "../../api/user";

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('账号不能为空'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (!isvalidPass(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    const validateCaptcha = (rule, value, callback) => {
      if (!isValidateCaptcha(value)) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: false,
      registerForm: {
        username: '',
        password: '',
        nickName: '',
        confirmPassword: '',
        phone: '',
        email: '',
      },
      loginForm: {
        username: '',
        password: '',
        captcha: ''
      },
      registerRules: {
        username: [{required: true, trigger: 'blur', message: '账号不能为空'}],
        password: [{required: true, trigger: 'blur', message: '密码不能为空'}],
        confirmPassword: [{required: true, trigger: 'blur', message: '确认密码不能为空'}]
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePass}],
        captcha: [{required: true, trigger: 'blur', validator: validateCaptcha}]
      },
      loading: false,
      pwdType: 'password',
      login_center_bg: login_center_bg,
      imgSrc: getBaseUri() + 'api/user/code'
    }
  },
  created() {
    const _this = this
    document.onkeydown = function (e) {
      let key
      if (window.event === undefined) {
        key = e.keyCode
      } else {
        key = window.event.keyCode
      }
      // enter的ASCII码是13
      if (key === 13) {
        _this.handleLogin()
      }
    }
  },
  methods: {
    handleRegister() {
      console.log(this.registerForm)
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          if (this.registerForm.password !== this.registerForm.confirmPassword) {
            this.$message({
              message: '两次密码不一致',
              type: 'error'
            })
          } else {
            this.loading = true
            userPostRequire("register", this.registerForm).then(response => {
              if (response) {
                this.$message({
                  message: response.message,
                  type: response.status ? 'success' : 'error'
                })
                this.dialogFormVisible = false
              }
              this.loading = false
            })
          }
        } else {
          console.log('参数验证不合法！')
          return false
        }
      })
    },
    changeCode() {
      this.imgSrc = getBaseUri() + 'api/user/code?t=' + new Date().getTime()
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // this.loginForm.password = doEncryptBySM3(this.loginForm.password)
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({path: '/home'})
          }).catch(() => {
            this.loading = false
          }).finally(() => {
            this.loginForm.password = ''
          })
        } else {
          console.log('参数验证不合法！')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border-top: 10px solid #409EFF;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409EFF;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>
